home *** CD-ROM | disk | FTP | other *** search
- Path: news.ultranet.com!usenet
- From: Ken Brady <kbrady@ultranet.com>
- Newsgroups: comp.lang.c++
- Subject: Are ostream.write and istream.read non-reciprocal??
- Date: Mon, 8 Jan 1996 08:53:02 EST
- Organization: UltraNet Communications, Inc.
- Message-ID: <60ii536@kbrady.ultranet.com>
- Reply-To: Ken Brady <kbrady@ultranet.com>
- NNTP-Posting-Host: 205.246.13.244
- X-Mailer: Post Road Mailer (Green Edition Ver 1.05a)
-
- I have an array of char which I want to store on a stream. The array
- is a string, 0-terminated; however, the length of the array may be
- longer than the length of the string, i.e., I may have some binary data
- following the string. I use
-
- { ...
- int len = ... // whatever length I need
- char *p = new p[len]
- // move binary data into p; may include zero values
- fstream f(fname,ios::trunc | ios::in | ios::out | ios::binary);
- f.write((char*)(&len),sizeof(len));
- f.write(p,len);
- ..}
-
- Later, to retrieve the data, I use
-
- {...
- fstream f(fname,ios::in | ios::binary);
- f.read((char*)(&len),sizeof(len));
- p = new char[len+1];
- f.read(p,len);
- ..}
-
-
- While the value of 'len' is correct after reading it from the istream,
- I find that after reading the character array my stream is out of
- registration, i.e., I've read too few or too many bytes, and the
- subsequent data on the stream cannot be read. The stream seems to be
- doing some sort of string-formatting even though I've specified it as
- binary.
-
- Are ostream.write and istream.read non-reciprocal?? How can I write a
- simple array of arbitrarily-valued char without resorting to a slow
- loop of put(p[i])??
-
- Advice much appreciated ...
-
-
-
- __________ `-'.
- /| o o O o ___:
- \ O ____0 /
- (//W ||
- ||| || -CyberPriestess-
- "" ""
- DONT HAVE A COW, MAN
-
-
-